﻿Imports BUS
Imports DTO
Public Class frmCapNhatSach : Inherits DevComponents.DotNetBar.Office2007Form

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub

    Dim lsach As List(Of CSachDTO)
    Private Sub Reload()
        Dim stt As Integer = 0
        lsach = New CSachBUS().LayDanhSachSach()
        dgvChiTiet.Rows.Clear()
        For Each sach As CSachDTO In lsach
            stt += 1
            dgvChiTiet.Rows.Add(stt, sach.Ma, sach.Ten, sach.TacGia, New CTheLoaiSachBUS().LayTenTheLoaiSachTheoMa(sach.MaTheLoai), sach.MaTinhTrang)
        Next
        cboTinhTrang.SelectedIndex = 0
    End Sub
    Private Sub frmCapNhatSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tinhtrang As List(Of CTinhTrangSachDTO) = New CTinhTrangSachBUS().LayDanhSachTinhTrangSach()
        For Each tt As CTinhTrangSachDTO In tinhtrang
            cboTinhTrang.Items.Add(tt)
        Next
        cboTinhTrang.DisplayMember = "Ten"
        cboTinhTrang.ValueMember = "Ma"
        Reload()
    End Sub

    Private Sub dgvChiTiet_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvChiTiet.CellClick
        If e.RowIndex > dgvChiTiet.Rows.Count Or e.RowIndex = -1 Then
            Return
        End If
        Dim r As DataGridViewRow = dgvChiTiet.Rows(e.RowIndex)
        txtMaSach.Text = r.Cells("clMa").Value
        txtTenSach.Text = r.Cells("clTenSach").Value
        txtTenTacGia.Text = r.Cells("clTenTacGia").Value
        txtTheLoai.Text = r.Cells("clTheLoai").Value
        Dim n As Integer = 0
        For Each tt As CTinhTrangSachDTO In cboTinhTrang.Items
            If tt.Ma = r.Cells("clMaTinhTrang").Value Then
                Exit For
            End If
            n += 1
        Next
        cboTinhTrang.SelectedIndex = n
    End Sub

    Public Sub Reset()
        txtMaSach.Text = ""
        txtTenSach.Text = ""
        txtTenTacGia.Text = ""
        txtTheLoai.Text = ""
        cboTinhTrang.SelectedIndex = 0
    End Sub
    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Reset()
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        If txtMaSach.Text = "" Then
            Return
        End If
        Dim sach As New CSachDTO()
        sach.Ma = txtMaSach.Text
        Dim tt As CTinhTrangSachDTO = cboTinhTrang.SelectedItem
        sach.MaTinhTrang = tt.Ma
        Dim kt As Boolean = New CSachBUS().CapNhatSach(sach)
        MessageBox.Show("Cập nhật thành công")
        Reload()
    End Sub
End Class